#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Solution
{
public:
    int Xindex[4] = {1, -1, 0, 0};
    int Yindex[4] = {0, 0, 1, -1};
    int Max = 0;

    void dfs(vector<vector<int>> &grid, int i, int j, std::vector<std::vector<bool>> &checkgrid, int m, int n, int &Count)
    {
        checkgrid[i][j] = true;
        Count++;
        for (int k = 0; k < 4; k++) // 每次都进行上下左右检查
        {

            int x = i + Xindex[k];
            int y = j + Yindex[k];
            if (x >= 0 && x < m && y >= 0 && y < n && !checkgrid[x][y] && grid[x][y] == 1)
                dfs(grid, x, y, checkgrid, m, n, Count);
        }
        Max = max(Count, Max);
    }

    int maxAreaOfIsland(vector<vector<int>> &grid)
    {
        // 遍历整个vector数组
        int m = grid.size();
        int n = grid[0].size();
        std::vector<std::vector<bool>> checkgrid(m, std::vector<bool>(n, false));

        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (!checkgrid[i][j] && grid[i][j] == 1)
                {
                    int Count = 0;
                    dfs(grid, i, j, checkgrid, m, n, Count);
                }
            }
        }
        return Max;
    }
};